package com.qingtian.example.web.controller;

import com.qingtian.example.ext.common.constant.HttpCode;
import com.qingtian.example.ext.utils.JsonUtils;
import com.qingtian.example.web.entity.People;
import com.qingtian.example.web.service.PeopleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @Author qingtian
 * @Description
 * @Date Created in 2018/3/15
 */
@RestController
@RequestMapping("/peoples")
public class PeopleController {

    public static final Logger logger = LoggerFactory.getLogger(PeopleController.class);

    @Autowired
    private PeopleService peopleService;

    /**
     * 列举所有people的列表
     *
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String listAll(@RequestParam(value = "offset", required = false) Integer offset,
                          @RequestParam(value = "limit", required = false) Integer limit) {

        //设置offset参数
        if (offset == null || offset < 1) {
            offset = 0;
        }
        //设置limit参数
        if (limit == null || limit < 1) {
            limit = 10;
        }
        //查询列表
        List<People> peopleList = peopleService.listAll(offset, limit);
       return JsonUtils.genInfoJsonStr(HttpCode.HTTP_OK, "获取列表数据成功", peopleList);
    }

    /**
     * 新增一条数据
     * @param name
     * @return
     */
    @RequestMapping(value = "/",method = RequestMethod.POST)
    public String save(@RequestParam(value = "name") String name) {

        //设置参数
        People entity = new People();
        entity.setName(name);
        //新增数据
        try {
            entity = peopleService.add(entity);
            return JsonUtils.genInfoJsonStr(HttpCode.HTTP_CREATE,"新增数据成功",entity);
        } catch (Exception e) {
            logger.error("class PeopleController method save execute exception [" + e.getMessage() + "]");
            return JsonUtils.genInfoJsonStr(HttpCode.HTTP_INVALID_REQUEST,"新增数据失败",e.getMessage());
        }
    }

    /**
     * 获取单条数据
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public String getPeople(@PathVariable("id")Long id){

        People entity = peopleService.getById(id);
        return JsonUtils.genInfoJsonStr(HttpCode.HTTP_OK,"获取数据成功",entity);
    }


    /**
     * 更新数据
     * @param id
     * @param name
     * @return
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    public String update(@PathVariable("id")Long id,
                         @RequestParam(value = "name")String name){
        People entity = new People();
        entity.setId(id);
        entity.setName(name);
        try {
            entity = peopleService.update(entity);
            return JsonUtils.genInfoJsonStr(HttpCode.HTTP_CREATE,"修改信息成功",entity);
        } catch (Exception e) {
            logger.error("class PeopleController method update execute exception [" + e.getMessage() + "]");
            return JsonUtils.genInfoJsonStr(HttpCode.HTTP_INVALID_REQUEST,"修改信息失败",e.getMessage());
        }
    }

    /**
     * 删除信息成功
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public String delete(@PathVariable("id")Long id){

        try {
            People entity = peopleService.deleteById(id);
            return JsonUtils.genInfoJsonStr(HttpCode.HTTP_NO_CONTENT,"删除信息成功",entity);
        } catch (Exception e) {
            logger.error("class PeopleController method delete execute exception [" + e.getMessage() + "]");
            return JsonUtils.genInfoJsonStr(HttpCode.HTTP_INVALID_REQUEST,"删除信息失败",e.getMessage());
        }
    }
}
